@extends('layouts.header')
@section('title','原始记录')
@section('style')
    .check-box {
    padding-left: 18px;
    padding-right: 0px;
    padding-top: 0px;
    padding-bottom: 0px;
    }
@endsection
@section('content')
    <nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页
        <span class="c-gray en">&gt;</span> 检测业务
        <span class="c-gray en">&gt;</span> <a onclick="location.href='/{{config('constant.name_admin')}}/contract/indexResLuru';">结果录入</a>
        <span class="c-gray en">&gt; 点击进入【{{optional($contractInfo)->client_name}}】</span> 
        <a class="btn btn-primary radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新"><i class="Hui-iconfont">&#xe68f;</i></a>
        <a class="btn btn-primary radius r mr-10" style="line-height:1.6em;margin-top:3px" href="javascript:self.location=document.referrer;"> <i class="Hui-iconfont Hui-iconfont-arrow1-left"></i>&nbsp;&nbsp;返回
        </a>
    </nav>
    <div class="page-container">
        <div class="cl pd-5 bg-1 bk-gray">
            <a class="btn btn-primary radius" href="javascript:;" onclick="submit_batch()">批量提交</a>
            <a class="btn btn-primary radius" href="javascript:;" id="luru-batch">批量录入</a>
            <a class="btn btn-primary radius" href="javascript:;" id="luru-date">批量录入试验日期</a>
            <a class="btn btn-primary radius" href="javascript:;" onclick="download_original({{ $contractInfo->id }},1)"><i class="Hui-iconfont">&#xe640;</i> 原始记录</a>
            <label><input type="checkbox" id="to-page" checked>分页</label>
        </div>
        <div class="tb_con">
            <div class="cl pd-5 bg-1 bk-gray" id="toolGroup"> </div>
            <div class="mt-10">
                <table class="table table-border table-bordered table-bg table-hover table-responsive" id="datatable">
                    <thead>
                        <tr class="text-c">
                            <th> <input type="checkbox" id="checkAll" > </th>
                            <th>条形码</th>
                            @if($enable_tool_ID)
                            <th>RFID</th>
                            @endif
                            <th>来源ID</th>
                            <th class="tool_name">样品名称</th>
                            <th>额定电压</th>
                            <th>样品状态</th>
                            <th>结论</th>
                            <th style="width: 100px;">不合格照片</th>
                            <th>状态</th>
                            <th>试验日期</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($res_data as $row)
                        @php 
                            $tool_name = $row['contract_gaiyao']['tool_name']; 
                            array_set($toolCount,$tool_name,array_get($toolCount,$tool_name,0) + 1); 
                        @endphp
                        <tr class="text-c">
                            <td>@if($row['status'] != 1)<input type="checkbox" value="{{$row['id']}}" /> @endif </td>
                            <td>{{$row['bar_code']}}</td>
                            @if($enable_tool_ID)
                            <td>{{$row['gqj_ID']}}</td>
                            @endif
                            <td>{{$row['third_id']}}</td>
                            <td>{{$row['contract_gaiyao']['tool_name']}}</td>
                            <td>{{$row['contract_gaiyao']['voltage']}}</td>
                            <td class="sample_status">{{$row['sample_status']}}</td>
                            <td>
                                @if ($row['res'] == 0)
                                    试验中
                                @elseif ($row['res'] == 1)
                                    <span class="label label-primary radius">合格</span>
                                @elseif ($row['res'] == 2)
                                    <span class="label label-danger radius">不合格</span>
                                    <br />
                                    @if(config('constant.app_name') === 'ShanDongZiBoQianHeng')
                                    <a class="ml-5 mt-5 btn btn-primary-outline radius size-MINI" href="javascript:;" onclick="uploadnonimg({{$row['id']}},`{{$row['bar_code']}}`,1)">上传外观照</a>
                                    <br />
                                    @endif
                                    <a class="ml-5 mt-5 btn btn-primary-outline radius size-MINI" href="javascript:;" onclick="uploadnonimg({{$row['id']}},`{{$row['bar_code']}}`,0)">上传不合格照</a>
                                @endif
                            </td>
                            <td style="widtd: 150px;">
                                <div id="non-photo-{{$row['id']}}">
                                    @php 
                                        //因为有些地方不合格照片是用的两张，所以这里用数组处理
                                        $non_photos = explode(',',$row['non_photo']);
                                        $count = count($non_photos);
                                        $width = 12/$count;
                                    @endphp
                                    @foreach($non_photos as $k => $photo)
                                        @if($photo)
                                            <div class="col-sm-{{$width}}">
                                                <img  class="img-responsive radio" src="{{$photo}}?time=time()" onclick="biggerimg(this)">
                                                <br>
                                                <a onclick="imgdel(`{{$row['bar_code']}}`,{{$k}},this)" class="ml-5 btn btn-danger-outline radius size-MINI">删除</a>
                                            </div>
                                        @endif
                                    @endforeach
                                </div>
                            </td>
                            <td>
                                @if ($row['status0'] == -1)
                                    <span class="label label-danger radius">被驳回</span>
                                @elseif ($row['status0'] == 0)
                                    <span class="label label-primary radius">未提交</span>
                                @elseif ($row['status0'] == 1)
                                    <span class="label label-default radius">已提交</span>
                                @elseif ($row['status0'] == 2)
                                    <span class="label label-success radius">审核通过</span>
                                @elseif ($row['status0'] == 3)
                                    <span class="label label-success radius">未试验</span>
                                @elseif ($row['status0'] == 4)
                                    <span class="label label-success radius">试验中</span>
                                @else
                                    <span class="badge badge-default radius">未知状态</span>
                                @endif
                            </td>
                            <td>{{$row['test_time']}}</td>
                            <td>
                                <div class="f-l">
                                    <a class="ml-5 btn btn-primary-outline radius size-MINI" href="javascript:;" onclick="layer_show('结果录入',`/{{config('constant.name_admin')}}/itemData/index?id={{$row['id']}}`)">结果录入</a>
                                    @if ($row['status'] == 1)
                                        <a class="ml-5 btn radius size-MINI disabled" href="javascript:;">提交</a>
                                    @else
                                        <a class="ml-5 btn btn-primary-outline radius size-MINI" href="javascript:;" onclick="submit_one({{$row['id']}})">提交</a>
                                    @endif
                                    @if ($row['status0'] == -1)
                                        <a class="ml-5 btn btn-danger radius size-MINI" href="javascript:;" onclick="show_baklog({{$row['id']}})">驳回原因</a>
                                    @endif
                                    <a class="ml-5 btn btn-primary radius size-MINI" href="javascript:;" onclick="download_original({{$row['id']}})"><i class="Hui-iconfont">&#xe640;</i>原始记录</a>
                                </div>
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
                <div class="f-r mt-10"><input class="input-text" type="text" value="" placeholder="指定页码" id="jumpPage"/></div>
            </div>
        </div>
    </div>

    <!--请在下方写此页面业务相关的脚本-->
    <script type="text/javascript" src="/admin/lib/jquery.mousewheel.min.js"></script>
    <script type="text/javascript" src="/admin/static/myfolder/js/imgopreate.js?ver={{config('constant.static_res_ver')}}"></script>
    <script type="text/javascript">
        var table, nophonotext = '未上传不合格照', index2, isup = 0;
        $(function () {
            $.fn.dataTable.ext.errMode = 'throw';//datatable抛异常的方式，在console中输出，默认以alert的方式弹出
            setTable();
            var toolCount = @json($toolCount);
            for(let tool_name in toolCount){
                $('#toolGroup').append('<span class="ml-10 " onclick="searchTool(this)"><span class="tool-name">'+tool_name+'</span>('+toolCount[tool_name]+')</span>');
            }
        });
        function setTable(page=true){
            // console.log(table);
            if (table != undefined) {
                table.destroy();
            }
            let dom = 'lftip';
            if (page == true) {
                $('#jumpPage').show();
            }else{
                dom = 'ift';
                $('#jumpPage').hide();
            }
            let attrs = {
                    dom:dom,
                    paging:page,
                    scrollY:500,
                    scrollCollapse:true,
                    language: {url: '/admin/lib/datatables/dataTables_Chinese_Lang.json'},
                    columnDefs : [ 
                        {
                            targets : 0,
                            "orderable" : false
                        },
                        {
                            targets : -1,
                            "orderable" : false
                        }
                    ],
                    order: [[ 1, "desc" ]],
                    // bStateSave:true
                };
            table = $('#datatable').DataTable(attrs);
        }

        $('#to-page').on('change',function () {
            let check = $(this).prop('checked');
            setTable(check);
        })

        function searchTool(obj) {
            $(obj).css('color', '#5a98de')
            $(obj).siblings().css('color', '#000')
            let toolname = $(obj).find('.tool-name').text(),
                index = $(obj).closest('div.tb_con').find('thead th.tool_name').index();
            // console.log(index);
            $('input[type=search]').val(toolname);
            table.column(index).search('^' + toolname + '$', true, false).draw();
        }

        /*批量录入*/
        $('#luru-batch').on('click',function () {
            layer_show('批量录入',"/{{config('constant.name_admin')}}/contractGaiyao/resLuru/{{request('id')}}",800);
        })

        function batch_submit() {
            var doms = $("tbody input:checkbox:checked");
            var l = doms.length;

            layer.msg(l + '', {time: 2000});
        }

        /*表格重新加载*/
        function table_reload(bool) {
            location.reload();
        }

        /*单个提交*/
        function submit_one(id) {
            let params = {
                    id: id,
                    batch_id: "{{request('id')}}",
                };
            ajax('post','/{{config('constant.name_admin')}}/sampleInfo/oneSubmit',params,submit_one_bak,0);
            function submit_one_bak(data) {
                if (data === '1') {
                    table_reload(0);//0停留在当前页，1回到第一页
                    layer.msg('提交成功!', {icon: 1, time: 1000});
                } else if (data === '2') {
                    layer.msg('结果未全部录入，不能提交!', {icon: 2, time: 2000});
                } else if (data === '0') {
                    layer.msg('提交失败!', {icon: 2, time: 2000});
                } else {
                    layer.msg(data, {icon: 2, time: 2000});
                }
            }
        }

        /*批量提交*/
        function submit_batch() {
            var doms = $("tbody tr > td:first-child input:checkbox:checked");
            var l = doms.length;
            if (l == 0) {
                layer.msg('请先勾选一条记录', {time: 2000});
                return;
            }

            var checkID = [];//定义一个空数组
            doms.each(function (i) {//把所有被选中的复选框的值存入数组
                checkID[i] = $(this).val();
            });

            var index2;
            layer.confirm('您勾选了 ' + l + ' 条记录<br>确定要批量提交吗？', {title: '询问', icon: 3}, function (index) {
                let params = {
                        checkID: checkID,
                        batch_id: "{{request('id')}}",
                    };
                ajax('post','/{{config('constant.name_admin')}}/sampleInfo/batchSubmit',params,submit_batchBak);
                function submit_batchBak(res) {
                    location.reload();
                }
            });
        }

        /*删除*/
        function del(id) {
            var index2;

            layer.confirm('确认要删除吗？', function (index) {
                $.ajax({
                    type: 'GET',
                    url: '/{{config('constant.name_admin')}}/client/del' + '?id=' + id,
                    // dataType: 'json',
                    success: function (data) {
                        // console.log(data);
                        if (data === '1') {
                            table_reload(0);//0停留在当前页，1回到第一页
                            layer.msg('删除成功!', {icon: 1, time: 2000});
                        } else {
                            layer.msg('删除失败!', {icon: 2, time: 2000});
                        }
                    },
                    error: function (data) {
                        ajax_error(data);
                    },
                    beforeSend: function () {
                        //0.4透明度 白色遮罩
                        index2 = layer.load(2, {shade: [0.05, '#000']});
                    },
                    complete: function () {
                        layer.close(index2);
                    },
                });
            });
        }

        /*编辑*/
        function edit(title, url, id, w, h) {
            layer_show(title, url + '?id=' + id, w, h);
        }

        /*添加*/
        function add(title, url, w, h) {
            layer_show(title, url, w, h);
        }

        /*上传不合格照片*/
        function uploadnonimg(id, bar_code, pos=0) {  //0:第一张是细节照，1:第二张是外观照片
            choseImgs(id, bar_code, pos);
        }

        function imgdel(bar_code,k, e) {
            $.ajax({
                type: 'get',
                url: '/{{config('constant.name_admin')}}/sampleinfo/imgdeal/non_photo/del/' + bar_code+'?k='+k,
                success: function (data) {
                    if (data === '1') {
                        $(e).parent().html(nophonotext);
                        layer.msg('删除成功!', {icon: 1, time: 1000});
                    } else {
                        layer.msg('删除失败!', {icon: 2, time: 2000});
                    }
                },
                error: function (data) {
                    ajax_error(data);
                },
                beforeSend: function () {
                    //0.4透明度 白色遮罩
                    index2 = layer.load(2, {shade: [0.05, '#000']});
                },
                complete: function () {
                    layer.close(index2);
                },
            });
        }

        /*查看驳回原因*/
        function show_baklog(id) {
            let url = '/{{config('constant.name_admin')}}/sampleinfo/rejectLog/0/' + id;
            layer_show('驳回原因', url, 500, 400);
        }

        /*下载原始记录*/
        function download_original(id,is_batch=0) {
            if (is_batch == 0) {
                window.open('/{{config('constant.name_admin')}}/exportReport/origin?id=' + id, '_blank');
            }else{
                window.open('/{{config('constant.name_admin')}}/exportReport/origin?contract_id=' + id, '_blank');
            }
            
        }

         /*批量录入试验日期*/
        $('#luru-date').on('click',function () {
            if($('tbody input:checkbox:checked').length == 0){
                layer.msg('请选择要修改的样品！');
                return false;
            }
            var html = "<div class='ml-20'><input id='pi_date' class='mt-10' type='date' /></div>";
            var indexDate = layer.open({
                type: 1,
                offset: 'auto',
                area: ['300px', '300px'],
                title: "批量录入试验日期", //不显示标题
                content: html, //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
                btn: ['提交', '取消'],
                yes: function (index, layero) {
                    var sample_ids = [];
                    $('tbody input:checkbox:checked').each(function(index){
                        sample_ids.push($(this).val());
                    })
                    var params = {
                        editDate:1,
                        item: 1, //同步项目时间
                        date:$('#pi_date').val(),
                        sample_ids:sample_ids,
                        _form_token:'{{csrf_form_token()}}'
                    };
                    layer.confirm('所有项目日期都会同步！',{
                        btn:['确定','取消'],
                    },function () {
                        luruDate(params);
                    },function () {
                        layer.close(indexDate);
                    })
                },
                cancel: function () {
                    //右上角关闭回调
                }
            });
        })

        function luruDate(params){
            ajax('post','',params,function (res) {
                if(res == 0){
                    layer.msg('操作失败！',{icon:2,time:1000},function (){
                        location.reload()
                    });
                }else{
                    layer.msg('操作成功！',{icon:1,time:1000},function (){
                        location.reload()
                    });
                }
            },0);
        }
    </script>
@endsection
